package com.acheli.registry.network.packet;

import com.acheli.registry.network.ACHMessages;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.simple.SimpleChannel;

import java.util.function.Supplier;

import static com.acheli.registry.entity.ACHEntities.RIDEABLE_COMPONENT_ENTITY_TYPE;

public class ExampleC2SPacket { //演示网络包
    public ExampleC2SPacket () {

    }

    public ExampleC2SPacket(FriendlyByteBuf buf) {

    }

    public void toBytes(FriendlyByteBuf buf) {

    }

    public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            // 服务器
            ServerPlayer player = context.getSender();
            if (player != null) {
                ServerLevel level = player.serverLevel().getLevel();

                //生成哈基米
                EntityType.CAT.spawn(level, (CompoundTag) null,null, player.blockPosition(),
                        MobSpawnType.COMMAND, true, false);

            }

        });
        return true;
    }

    public static void register(SimpleChannel channel) {
        channel.messageBuilder(ExampleC2SPacket.class, ACHMessages.id(), NetworkDirection.PLAY_TO_SERVER)
                .decoder(ExampleC2SPacket::new)
                .encoder(ExampleC2SPacket::toBytes)
                .consumerMainThread(ExampleC2SPacket::handle)
                .add();
    }
}
